<%@page contentType="text/html; charset=utf-8"%>
<%@ include file="../inc/TagLibs.jsp"%>
<c:set var="pageTitle" value="短信发送" />
<%@ include file="../inc/Title.jsp"%>
<head>
<link rel="stylesheet" type="text/css" href="/css/base.css" />
<link href="/calendar/dhtmlgoodies_calendar/dhtmlgoodies_calendar.css" rel="stylesheet" type="text/css">
<script src="/js/popup_ext.js"></script>
<script src="/calendar/dhtmlgoodies_calendar/dhtmlgoodies_calendar.js"></script>
<script language="javascript">
	function addReceiver(name,code){
		var r=document.getElementById("receiver");
		var v=r.value;
		if(v==null || v==""){
			r.value=code+"<"+name+">";
		}else{
			v=v+"\n"+code+"<"+name+">";
			r.value=v;
		}
		r.scrollTop=r.scrollHeight;
		
		var num=Number(document.getElementById("receiverNumSpan").innerHTML)+1;
		document.getElementById("receiverNumSpan").innerHTML=num;
	}
	function doSend(){
		disabledButton(true);
		var r=document.getElementById("receiver").value;
		if(r==null || r==""){
			ShowAlert('提示框','请输入接收人手机号码',200,100);
			disabledButton(false);
			return;
		}
		if(!checkContent()){
			disabledButton(false);
			return;
		}
		if(document.getElementById("planTimeCb").checked){
			var pd=document.getElementById("planTimeDate").value;
			if(pd==null || pd==""){
				ShowAlert('提示框','请输入定时发送时间',200,100);
				disabledButton(false);
				return;
			}else if(!checkDateStr(pd)){
				disabledButton(false);
				return;
			}
		}
		if(!checkSign()){
			disabledButton(false);
			return;
		}
		checkBalance("doSendFun()","disabledButton(false);");
	}
	function checkDateStr(dateStr){
		var arr=dateStr.split("-");
		if(arr.length==null || arr.length!=3){
			ShowAlert('提示框','定时发送时间格式不正确，正确格式：2010-01-01',200,100);
			return false;
		}else if(isNaN(arr[0]) || isNaN(arr[1]) || isNaN(arr[2])){
			ShowAlert('提示框','定时发送时间格式不正确，正确格式：2010-01-01',200,100);
			return false;
		}
		return true;
	}
	function checkSign(){
		var signTxt=document.getElementById("sign").value;
		if(!document.getElementById("signCb").checked || signTxt==null || signTxt.trim()==""){
			ShowAlert('提示框','尊敬的客户：为规范企业短信应用，减少手机用户投诉，应运营商相关业务要求，所有移动106通道发出短信，必须在短信内容中体现发送企业签名！',300,200);
			return false;
		}else if(getSignLength()<2){
			ShowAlert('提示框','短信签名不能少于两个汉字，请修改',200,100);
			return false;
		}else{
			return true;
		}
	}
	function getSignLength(){
		var signTxt=document.getElementById("sign").value.trim();
		var badChar ="ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
		badChar += "abcdefghijklmnopqrstuvwxyz"; 
		badChar += "0123456789"; 
		badChar += " "+"　";//半角与全角空格 
		badChar += "`~!@#$%^&()-_=+]\\\\|:;\"\\\'<,>?/*.";
		var len=0;
		for(var i=0;i<signTxt.length;i++){ 
			var c = signTxt.charAt(i);
			if(badChar.indexOf(c) > -1){ 
				len=len+1;
			}else{
				len=len+2;
			}
		}
		return len/2;
	}
	function doSendFun(){
		frm.submit();
	}
	
	function selectSmsPhrase(){
		var url="/base/SmsPhraseListForSelect.ihtm";
		ShowIframeNoScroll("选择常用短语",url,310,500,"selectSmsPhraseCallBack()",900,500);
	}
	function selectSmsPhraseCallBack(){
		if(popRetVal!=null){
			document.getElementById("content").value=popRetVal[0];
			countCharNum();
		}
	}
	function changePlanTimeCb(){
		if(document.getElementById("planTimeCb").checked){
			document.getElementById("planTimeButton").disabled=false;
			document.getElementById("planTimeHour").disabled=false;
			document.getElementById("planTimeMinute").disabled=false;
			document.getElementById("planTimeDate").className="TextBox Datetime";
			document.getElementById("planTimeDate").readOnly=false;//readOnly区分大小写
		}else{
			document.getElementById("planTimeButton").disabled=true;
			document.getElementById("planTimeHour").disabled=true;
			document.getElementById("planTimeMinute").disabled=true;
			document.getElementById("planTimeDate").className="TextBox Datetime readonly";
			document.getElementById("planTimeDate").readOnly=true;//readOnly区分大小写
		}
	}
	function changeSignCb(){
		if(document.getElementById("signCb").checked){
			document.getElementById("sign").readOnly=false;
			document.getElementById("sign").className="TextBox";
		}else{
			document.getElementById("sign").readOnly=true;
			document.getElementById("sign").className="TextBox readonly";
		}
	}
	function checkContent(){
		var c=document.getElementById("content").value;
		if(c==null || c==""){
			ShowAlert('提示框','请输入短信内容',200,100);
			return  false;
		}
		var s="";
		var cAll=c;
		if(document.getElementById("signCb").checked){
			s=document.getElementById("sign").value;
			if(s==null || s==""){
				ShowAlert('提示框','请输入短信签名',200,100);
				return false;
			}
			var smsSignFormat="${smsSignFormat}";
			cAll=cAll+smsSignFormat.replace("XXX",s);
		}
		if(cAll.length>134){
			ShowAlert('提示框','短信内容不能超过134个字符（包括短信签名）',200,100);
			return false;
		}
		if(cAll.length>67){
			var sCount=cAll.length/67;
			var sCountInt=parseInt(sCount);
			if(sCountInt<sCount){
				sCountInt=sCountInt+1;
			}
			ShowConfirm('确认框','每条短信(含签名档)最多只能67个字，本次短信发送字数为"+cAll.length+"个，将自动拆分为"+sCountInt+"条短信发送。确定发送吗？',340,160,"checkContentCallBack()");
		}
		return true;
	}
	function checkContentCallBack(){
		if(popRetVal!=null){
			return true;
		}else{
			return false;
		}
	}
	function doSendMe(){
		disabledButton(true);
		if(!checkContent()){
			disabledButton(false);
			return;
		}
		if(!checkSign()){
			disabledButton(false);
			return;
		}
		checkBalance("doSendMeFun();","disabledButton(false);");
	}
	function doSendMeFun(){
		var c=document.getElementById("content").value;
		var s="";
		if(document.getElementById("signCb").checked){
			s=document.getElementById("sign").value;
		}
		var xmlHttp=createXMLHttpRequest();
		xmlHttp.open("GET", "/sms/SmsSendMyself.ihtm?content="+encodeURIComponent(c)+"&sign="+encodeURIComponent(s),true);
		xmlHttp.onreadystatechange = function(){
			if (xmlHttp.readyState == 4) {
	      		if (xmlHttp.status == 200) {
					var ret=xmlHttp.responseText;
					var msg="";
					if(ret=="NO_MOBILE"){
						msg="对不起，您的员工资料还没有填写手机号码，请填写后重新登录管理易！";
					}else{
						msg="测试发给自己的短信已发送，请确认收到该短信再正式发送！";
					}
					ShowAlert('提示框',msg,200,100);
					disabledButton(false);
				}
			}
		};
		xmlHttp.send(null);
	}
	
	function disabledButton(flag){
		document.getElementById("testButton").disabled=flag;
		document.getElementById("sendButton").disabled=flag;
		document.getElementById("clearButton").disabled=flag;
	}
	
	function doCancel(){
		window.close();
	}
	function doStafferAll(){
		var ob_arr = document.getElementsByName("stafferHid");
		for (i=0;i<ob_arr.length;i++){
			addReceiver(ob_arr[i].getAttribute("s_name"),ob_arr[i].getAttribute("s_phone"));
		}
	}
	function doPhoneBookAll(){
		var ob_arr = document.getElementsByName("pbHid");
		for (i=0;i<ob_arr.length;i++){
			addReceiver(ob_arr[i].getAttribute("s_name"),ob_arr[i].getAttribute("s_phone"));
		}
	}
	function countCharNum(){
		var cont=document.getElementById("content").value;
		if(cont!=null && !cont==""){
			var sign=document.getElementById("sign").value;
			if(sign!=null && !sign==""){
				var smsSignFormat="${smsSignFormat}";
				cont=cont+smsSignFormat.replace("XXX",sign);
			}
		}
		document.getElementById("charNum").innerHTML="字数："+cont.length+"(含签名)";
	}
	function showMe(){
		var w=915;
		var normalHeiht=530;
		var alertHeight=normalHeiht+40;
		if(document.getElementById("errorMsgTd")!=null){
			window.resizeTo(w,alertHeight);
		}else{
			window.resizeTo(w,normalHeiht);
		}
	}
	function showBalance(){
		var xmlHttp=createXMLHttpRequest();
		xmlHttp.open("GET", "/sms/SmsGetBalance.ihtm",true);//true表示异步执行
		xmlHttp.onreadystatechange = function(){
			if (xmlHttp.readyState == 4) {
	      		if (xmlHttp.status == 200) {
					clearTimeout(clearTO);//下面定义的变量可以在这里用
	      			if(xmlHttp.responseText=="ERROR"){
	      				document.getElementById("balanceDiv").innerHTML="<span style='color:#ff0000'>无法连接到短信服务器，请稍候再连接！</span>";
					}else if(xmlHttp.responseText=="NO_SMS"){
						document.getElementById("balanceDiv").innerHTML="<span style='color:#ff0000'>短信未开通，请联系客服QQ 100763163开通！</span>";
					}else{
						var arr=xmlHttp.responseText.split(";");
	      				document.getElementById("balanceDiv").innerHTML="剩余点数："+arr[0]+"点 可发短信："+arr[1]+"条 [提醒：晚上9点至上午8点请不要发送短信]";
	      			}
				}
			}
		};
		var clearTO = setTimeout(function(){
			xmlHttp.abort();
			document.getElementById("balanceDiv").innerHTML="<span style='color:#ff0000'>无法连接到短信服务器，请稍候再连接！</span>";
		},1000*20);
		xmlHttp.send(null);
	}
	function checkBalance(funName,buttonFunName){
		var xmlHttp=createXMLHttpRequest();
		xmlHttp.open("GET", "/sms/SmsGetBalance.ihtm",true);//true表示异步执行
		xmlHttp.onreadystatechange = function(){
			if (xmlHttp.readyState == 4) {
	      		if (xmlHttp.status == 200) {
					clearTimeout(clearTO);//下面定义的变量可以在这里用
	      			if(xmlHttp.responseText=="ERROR"){
	      				ShowAlert('提示框','无法连接到短信服务器，请稍候再连接！',200,100);
						if(buttonFunName!=null && buttonFunName!=""){
							eval(buttonFunName);
						}
					}else if(xmlHttp.responseText=="NO_SMS"){
						ShowAlert('提示框','短信未开通，请联系客服QQ 100763163开通！',200,100);
						if(buttonFunName!=null && buttonFunName!=""){
							eval(buttonFunName);
						}
	      			}else{
						var arr=xmlHttp.responseText.split(";");
						if(arr[1]=="0"){
							ShowAlert('提示框','您的余额不足，请充值后再发送！',200,100);
							if(buttonFunName!=null && buttonFunName!=""){
								eval(buttonFunName);
							}
						}else{
							eval(funName);
						}
	      			}
				}
			}
		};
		var clearTO = setTimeout(function(){
			xmlHttp.abort();
			ShowAlert('提示框','无法连接到短信服务器，请稍候再连接！',200,100);
			if(buttonFunName!=null && buttonFunName!=""){
				eval(buttonFunName);
			}
		},1000*20);
		xmlHttp.send(null);
	}
	function doPhoneBookNew(){
		var url="/base/PhoneBookNew.ihtm?alert_type=NONE";
		ShowIframeNoScroll("新增联系人",url,320,440,"doPhoneBookNewCallBack()");
	}
	function doPhoneBookNewCallBack(){
		if(popRetVal!=null){
			//返回的是[perName];[mobile]
			var arr=popRetVal.split(";");
			var tbl=document.getElementById("phoneBookTbl");
			var tr=document.createElement("tr");
			tr.setAttribute("perName",arr[0]);
			tr.setAttribute("mobile",arr[1]);
			tr.onclick=elementOnClick;
			tr.style.cursor="pointer";
			tr.onmouseout=elementOnMouseOut;
			tr.onmouseover=elementOnMouseOver;
			var th=document.createElement("th");
			th.appendChild(document.createTextNode(arr[0]));
			var td=document.createElement("td");
			td.appendChild(document.createTextNode(arr[1]));
			tr.appendChild(th);
			tr.appendChild(td);
			tbl.appendChild(tr);
			document.getElementById("phoneBookDiv").scrollTop=document.getElementById("phoneBookDiv").scrollHeight;
			
			addReceiver(arr[0],arr[1]);
		}
	}
	function elementOnClick(){
		addReceiver(this.perName,this.mobile);
	}
	function elementOnMouseOut(){
		this.style.backgroundColor='';
	}
	function elementOnMouseOver(){
		this.style.backgroundColor='#5399D0';
	}
	function countReceiver(){
		var arr=document.getElementById("receiver").value.split("\n");
		var num=0;
		for(var i=0;i<arr.length;i++){
			if(arr[i].trim()!=""){
				num++;
			}
		}
		document.getElementById("receiverNumSpan").innerHTML=num;
	}
	function clearReceiver(){
		document.getElementById('receiver').value='';
		document.getElementById("receiverNumSpan").innerHTML="0";
	}
	function doInit(){
		showMe();
		showBalance();
		countCharNum();
		countReceiver();
		document.getElementById("content").focus();
	}
</script>
</head>
<body oncontextmenu="return false" class="sheet modal-dialog" style="text-align:center;" onLoad="doInit();" scroll="no">
<form name="frm" action="SmsSend.ihtm" method="post">
<div class="sheet-main" style="width:870px;text-align:left;margin:auto;">
  <div class="sheet-top">
    <div class="content chit">
      <h2>短信发送</h2>
      <p><div id="balanceDiv"><span>正在连接短信服务器，请稍候...</span></div></p>
    </div>
  </div>

  <c:if test="${alertMsg!=null && alertMsg!=''}">
	<div class="msg-error" id="errorMsgTd">
		<c:if test="${alertMsg=='OK'}">短信已经成功添加到服务器发送队列，您可以<a href="javascript:showWin(900,500,'/sms/SmsMyHistoryList.ihtm');">查看当前发送情况</a></c:if>
		<c:if test="${alertMsg!='OK'}">${alertMsg}</c:if>
		<c:remove var="alertMsg" scope="session" />
	</div>
  </c:if>
  
  <div class="sheet-list">
    <div class="modal-main search-business chit">
	
	     <div class="chit-send-info">
		 	<p class="chit-title">
				<a href="javascript:doStafferAll();" style="float:right;">全部加入&gt;&gt;</a>
				同事：
			</p>
			<div class="chit-box">
				<table width="161">
					<c:forEach var="item" items="${stafferList}">
					<tr onClick="addReceiver('<c:out value="${item.stafferName}" />','<c:out value="${item.mobile}" />');" style="cursor:pointer;" onmouseout="this.style.backgroundColor=''" onmouseover="this.style.backgroundColor='#5399D0'"><th><c:out value="${item.stafferName}" /></th><td><c:out value="${item.mobile}" /></td></tr>
					<input type="hidden" name="stafferHid" s_name="<c:out value="${item.stafferName}" />" s_phone="<c:out value="${item.mobile}" />">
					</c:forEach>
				</table>
			</div>
		 </div>
		 
		 <div class="chit-send-info">
			 <p class="chit-title">
				<a href="javascript:doPhoneBookAll();" style="float:right;">全部加入&gt;&gt;</a>
				通讯录：<a href="javascript:showWin(760,400,'/base/PhoneBookList.ihtm');"><img src="/images/icon_edit.gif" border="0"></a>
			</p>
			<div class="chit-box" id="phoneBookDiv">
				<table width="161">
					<tbody id="phoneBookTbl">
					<c:forEach var="item" items="${phoneBookList}">
					<tr onClick="addReceiver('<c:out value="${item.perName}" />','<c:out value="${item.mobile}" />');" style="cursor:pointer;" onmouseout="this.style.backgroundColor=''" onmouseover="this.style.backgroundColor='#5399D0'"><th><c:out value="${item.perName}" /></th><td><c:out value="${item.mobile}" /></td></tr>
					<input type="hidden" name="pbHid" s_name="<c:out value="${item.perName}" />" s_phone="<c:out value="${item.mobile}" />">
					</c:forEach>
					</tbody>
				</table>
			</div>
		 </div>
		 
		 <div class="chit-send-info2">
		 	<div class="chit-box">
				<div class="chit-box-info right">
					<p>短信内容：</p>
					<div class="dhit-send-txt">
						<div class="txt-info">
							<textarea name="content" id="content" class="TextBox" cols="" rows="" onKeyUp="countCharNum();"><c:out value="${content}" /></textarea>
							<input type="button" value="选择短语" class="buttom" onClick="selectSmsPhrase();" style="float:right; "/>
							<span id="charNum"></span>
						</div>
					</div>
				</div>
				<div class="chit-box-info left">
					<p>接收人手机号码：合计<span id="receiverNumSpan">0</span>个</p>
					<textarea name="receiver" id="receiver" class="TextBox" cols="" rows="" onChange="countReceiver();"><c:out value="${receiver}" /></textarea>
				</div>
				<div style="padding:0 6px;">
					<p>
						<label for="ds">
						<input type="checkbox" name="planTimeCb" id="planTimeCb" value="1" onClick="changePlanTimeCb();" />
						定时发送 时间：
						</label>
						<input type="text" maxlength="10" class="TextBox Datetime readonly" readOnly="true" name="planTimeDate" id="planTimeDate" value="${planTimeDate}" />
						<img src="/images/but-calen.gif" id="planTimeButton" name="planTimeButton" style="cursor:pointer" disabled="true" onClick="displayCalendar(document.getElementById('planTimeDate'),'yyyy-mm-dd',document.getElementById('receiver'));" />
						<select id="planTimeHour" name="planTimeHour" disabled="true">
							<c:forEach var="item" begin="0" step="1" end="23" varStatus="i"><c:if test="${i.index<10}"><c:set var="item" value="0${item}" /></c:if><option value="${item}" <c:if test="${item==planTimeHour}">selected</c:if>>${item}</option></c:forEach>
						</select>
						<select id="planTimeMinute" name="planTimeMinute" disabled="true">
							<c:forEach var="item" begin="0" step="1" end="59" varStatus="i"><c:if test="${i.index<10}"><c:set var="item" value="0${item}" /></c:if><option value="${item}" <c:if test="${item==planTimeMinute}">selected</c:if>>${item}</option></c:forEach>
						</select>
					</p>
					<p>
						<label for="qm">
						<input type="checkbox" name="signCb" id="signCb" value="1" onClick="changeSignCb();" checked=true readonly="true" />
						短信带签名：
						</label>
						&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" style="width:70px" onKeyUp="countCharNum();" class="TextBox" name="sign" id="sign" value="${sign}" />
					</p>
					<p style="padding-top:4px;"><font color="#666666">说明：定时发送时间必须至少在当前时间30分钟后。</font></p>
				</div>
			</div>
		 </div>
		 
	  </div>
	  <div class="modal-buttom" id="buttonDiv">
	  	<input type="button" class="buttom-a" id="testButton" value="测试发给自己" onClick="doSendMe();">
		<input type="button" class="buttom-a" id="sendButton" value=" 发 送 " onClick="doSend();">
		<input type="button" class="buttom-a" id="clearButton" value="清空接收人" onClick="clearReceiver();">
		<input type="button" class="buttom-a" value=" 取 消 " onClick="doCancel();">
		<input type="hidden" id="type" name="type" value="${type}">
	  </div>

  </div>
</div>
</form>
</body>
</html>